<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'winners';
	public function __construct()
	{
		parent::__construct();
		// Load all the required classes
		$this->load->library('form_validation');
		$this->load->model('stoepje_winners_m');
		$this->load->model('stoepje_vouchers_m');
		$this->load->model('stoepje_prizes_m');
		$this->lang->load('winner');
		
		$this->data->prizeList = array();
		if ($prizes = $this->stoepje_prizes_m->order_by('id')->get_all())
		{
			foreach ($prizes as $prize)
			{
				$this->data->prizeList[$prize->id] = $prize->name;
			}
		}
		$this->data->statusList = array(
			'1'		=> lang('stoepje_voucher_status_active_title'),
			'0'		=> lang('stoepje_voucher_status_inactive_title')	
		);
				
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('stoepje.winner.js', 'winner'))
						->append_metadata(css('stoepje.winner.css', 'winner'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		//set the base/default where clause
		$base_where = array();

		//add post values to base_where if f_module is posted
		$base_where = $this->input->post('f_prize') ? $base_where + array('f_prize' => $this->input->post('f_prize')) : $base_where;
		$base_where = $this->input->post('f_keywords') ? $base_where + array('keywords' => $this->input->post('f_keywords')) : $base_where;

		//Number of winner
		$numberOfWinner = $this->stoepje_winners_m->count_by();
		$numberOfTicketWinner = $this->stoepje_winners_m->count_by(array('f_prize' => 1));
		// Create pagination links
		$total_rows = $this->stoepje_winners_m->count_by($base_where);
		$pagination = create_pagination('admin/winner/index', $total_rows);

		// Using this data, get the relevant results
		$winners = $this->stoepje_winners_m->limit($pagination['limit'])->get_many_by($base_where);

		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';

		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('numberOfWinner', $numberOfWinner)
			->set('numberOfTicketWinner', $numberOfTicketWinner)
			->set('pagination', $pagination)
			->set('winners', $winners);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
	
	public function delete($id = 0)
	{
		// Delete one
		$ids = ($id) ? array($id) : $this->input->post('action_to');
	
		// Go through the array of slugs to delete
		if ( ! empty($ids))
		{
			$winner_names = array();
			foreach ($ids as $id)
			{
				// Get the current page so we can grab the id too
				if ($winner = $this->stoepje_winners_m->get($id))
				{
					$this->stoepje_winners_m->delete($id);
					$winner_names[] = $winner->first_name . ' ' . $winner->last_name;
				}
			}
		}
	
		// Some pages have been deleted
		if ( ! empty($winner_names))
		{
			// Only deleting one page
			if (count($winner_names) == 1)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('stoepje_winner_delete_success_label'), $winner_names[0]));
			}
			// Deleting multiple pages
			else
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('stoepje_winner_delete_mass_success_label'), implode('", "', $winner_names)));
			}
		}
		// For some reason, none of them were deleted
		else
		{
			$this->session->set_flashdata('notice', lang('stoepje_winner_delete_error_label'));
		}
	
		redirect('admin/winner');
	}
	public function action()
	{
		switch ($this->input->post('btnAction'))
		{			
			case 'delete':
				$this->delete();
				break;
			default:
				redirect('admin/winner');
			break;
		}
	}
}
